import api from './api';
import { Teacher } from '@/types';

// 接口返回类型
interface ApiResponse<T = any> {
  code: number;
  message: string; // 后端返回的是message而不是msg
  data?: T;
}

interface LoginResponse {
  token: string;
  teacher: Teacher;
}

// 教师注册
export const registerTeacher = async (teacherID: string, name: string, password: string): Promise<ApiResponse> => {
  try {
    console.log('教师注册:', { teacherID, name });
    const response = await api.post('/teacher/register', { 
      teacher_id: teacherID,
      name, 
      password 
    });
    console.log('教师注册响应:', response);
    return response;
  } catch (error) {
    console.error('教师注册错误:', error);
    throw error;
  }
};

// 教师登录
export const loginTeacher = async (teacherID: string, password: string): Promise<ApiResponse<LoginResponse>> => {
  try {
    console.log('教师登录:', { teacherID });
    const response = await api.post('/teacher/login', { 
      teacher_id: teacherID,
      password 
    });
    console.log('教师登录响应:', response);
    return response;
  } catch (error) {
    console.error('教师登录错误:', error);
    throw error;
  }
};

// 教师登出
export const logoutTeacher = async (): Promise<ApiResponse> => {
  try {
    const response = await api.post('/teacher/logout');
    return response;
  } catch (error) {
    console.error('教师登出错误:', error);
    throw error;
  }
}; 